﻿using EleCho.GoCqHttpSdk;
using Microsoft.Extensions.Logging;
using PluginSdk;
using System.Text;

namespace SystemStatus;

class Status
{
    public class StatusClass
    {
        public int ProcessorCount;
        public string OS_Env;
        public string OS_Rti;
        public string CLR_Env;
        public string CLR_Rti;
        public string OsArch;
        public string SysArch;
    }
    public static StatusClass GetStatus => new()
    {
        ProcessorCount = System.Environment.ProcessorCount,
        OS_Env = System.Environment.OSVersion.VersionString,
        CLR_Env = System.Environment.Version.ToString(),
        OS_Rti = System.Runtime.InteropServices.RuntimeInformation.OSDescription,
        CLR_Rti = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription,
        OsArch = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture.ToString(),
        SysArch = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString(),
    };
}

public class StatusCommand : ITextCommand
{
    public string Match => "^#status";

    public Permission Permission => Permission.SameUser;

    public string Name => "服务器状态";

    public string Description => "查询服务器状态";

    public async Task Handler(IMessageStream stream)
    {
        var status = Status.GetStatus;
        var sb = new StringBuilder();
        sb.AppendLine($"可用处理器数量：{status.ProcessorCount}");
        sb.AppendLine($"系统信息（环境变量）：{status.OS_Env}");
        sb.AppendLine($"系统信息（运行时参数）：{status.OS_Rti}");
        sb.AppendLine($"运行时信息（环境变量）：{status.CLR_Env}");
        sb.AppendLine($"运行时信息（运行时参数）：{status.CLR_Rti}");
        sb.AppendLine($"系统架构：{status.OsArch}");
        sb.AppendLine($"处理器架构：{status.SysArch}");
        await stream.WriteMessage(new(sb.ToString()));
    }
}

public class StatusPlugin : IPlugin
{
    public string Name => "状态";

    public string Description => "查询服务器状态";

    public ICommandBase[] Commands => [new StatusCommand()];

    public void Config()
    {
    }

    public void ConfigLogger(ILoggerFactory loggerFactory)
    {
    }

    public void ConfigSession(ICqActionSession session)
    {
    }
}